/*     */ package org.yaukie.auth.service.impl;
/*     */ 
/*     */ import java.io.PrintWriter;
/*     */ import java.io.StringWriter;
/*     */ import java.util.Date;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.security.authentication.AuthenticationManager;
/*     */ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
/*     */ import org.springframework.security.core.Authentication;
/*     */ import org.springframework.stereotype.Component;
/*     */ import org.yaukie.auth.constant.AjaxResult;
/*     */ import org.yaukie.auth.constant.AuthCons;
/*     */ import org.yaukie.auth.entity.LoginBody;
/*     */ import org.yaukie.auth.entity.LoginUser;
/*     */ import org.yaukie.auth.service.api.LoginHandlerService;
/*     */ import org.yaukie.base.exception.UserDefinedException;
/*     */ import org.yaukie.base.redis.RedisOrMapCache;
/*     */ import org.yaukie.base.system.ASyncFactory;
/*     */ import org.yaukie.base.system.ASyncManager;
/*     */ import org.yaukie.base.util.DateUtils;
/*     */ import org.yaukie.base.util.MessageTools;
/*     */ import org.yaukie.base.util.SpringContextUtil;
/*     */ import org.yaukie.base.util.StringTools;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Component
/*     */ public class LoginHandlerServiceImpl
/*     */   implements LoginHandlerService
/*     */ {
/*  37 */   private static final Logger log = LoggerFactory.getLogger(LoginHandlerServiceImpl.class);
/*     */ 
/*     */   
/*     */   @Autowired
/*     */   private TokenHandlerServiceImpl tokenHandlerService;
/*     */ 
/*     */   
/*     */   @Autowired
/*     */   private AuthenticationManager authenticationManager;
/*     */ 
/*     */   
/*     */   @Autowired
/*     */   private RedisOrMapCache redisCache;
/*     */   
/*     */   @Autowired
/*     */   private ASyncManager aSyncManager;
/*     */ 
/*     */   
/*     */   public String login(LoginBody loginBody) {
/*  56 */     String uuid = loginBody.getUuid();
/*  57 */     String capchaCodeKey = "captcha_codes:" + uuid;
/*  58 */     String code = loginBody.getVerifyCode();
/*  59 */     String codeOfRedis = this.redisCache.getCacheObject(capchaCodeKey) + "";
/*  60 */     if (StringTools.isEmpty(codeOfRedis)) {
/*  61 */       this.aSyncManager.execute(ASyncFactory.recordLoginInfo(loginBody.getUserName(), AuthCons.Login.LOGIN_FAIL.getDes(), MessageTools.message("yaukie.user.jcaptcha.expire", new Object[0]), new Object[0]));
/*  62 */       throw new UserDefinedException(MessageTools.message("yaukie.user.jcaptcha.expire", new Object[0]));
/*     */     } 
/*  64 */     this.redisCache.deleteObject(capchaCodeKey);
/*  65 */     if (!code.equals(codeOfRedis)) {
/*  66 */       this.aSyncManager.execute(ASyncFactory.recordLoginInfo(loginBody.getUserName(), AuthCons.Login.LOGIN_FAIL.getDes(), MessageTools.message("yaukie.user.jcaptcha.error", new Object[0]), new Object[0]));
/*  67 */       throw new UserDefinedException(MessageTools.message("yaukie.user.jcaptcha.error", new Object[0]));
/*     */     } 
/*     */ 
/*     */     
/*  71 */     Authentication authentication = null;
/*  72 */     String jwtToken = null;
/*     */     try {
/*  74 */       authentication = this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginBody
/*  75 */             .getUserName(), loginBody.getPassWord()));
/*  76 */       LoginUser loginUser = (LoginUser)authentication.getPrincipal();
/*  77 */       jwtToken = this.tokenHandlerService.buildToken(loginUser);
/*     */       
/*  79 */       SpringContextUtil.getRequest().getSession().setAttribute("currentUserName", loginUser.getXUser().getUserName());
/*  80 */       SpringContextUtil.getRequest().getSession().setAttribute("currentDeptName", "SmartKettle-演示部门");
/*  81 */       this.aSyncManager.execute(ASyncFactory.recordLoginInfo(loginBody.getUserName(), AuthCons.Login.LOGIN_SUCCESS.getDes(), MessageTools.message("yaukie.user.login.success", new Object[0]), new Object[0]));
/*  82 */       log.info("登录成功,会话将会在{}后过期", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date(loginUser.getExpireTime().longValue())));
/*  83 */     } catch (Exception ex) {
/*     */       
/*  85 */       if (ex instanceof org.springframework.security.authentication.BadCredentialsException) {
/*  86 */         this.aSyncManager.execute(ASyncFactory.recordLoginInfo(loginBody.getUserName(), AuthCons.Login.LOGIN_FAIL.getDes(), MessageTools.message("yaukie.user.password.not.match", new Object[0]), new Object[0]));
/*  87 */         throw new UserDefinedException(MessageTools.message("yaukie.user.password.not.match", new Object[0]));
/*     */       } 
/*  89 */       StringWriter stringWriter = new StringWriter();
/*  90 */       ex.printStackTrace(new PrintWriter(stringWriter));
/*  91 */       this.aSyncManager.execute(ASyncFactory.recordLoginInfo(loginBody.getUserName(), AuthCons.Login.LOGIN_FAIL.getDes(), stringWriter.toString().substring(0, 253), new Object[0]));
/*  92 */       throw new UserDefinedException(ex.getMessage());
/*     */     } 
/*     */ 
/*     */     
/*  96 */     return jwtToken;
/*     */   }
/*     */ 
/*     */   
/*     */   public AjaxResult logout(String token) {
/* 101 */     LoginUser loginUser = this.tokenHandlerService.getLoginUser(SpringContextUtil.getRequest());
/*     */     try {
/* 103 */       this.tokenHandlerService.delLoginUser(token);
/* 104 */     } catch (Exception ex) {
/* 105 */       this.aSyncManager.execute(ASyncFactory.recordLoginInfo(loginUser.getUsername(), AuthCons.Login.LOGIN_OUT_FAIL.getDes(), ex.getMessage(), new Object[0]));
/* 106 */       throw new UserDefinedException("LOGIN_MODULE", AuthCons.Login.LOGIN_OUT_FAIL.getDes());
/*     */     } 
/* 108 */     this.aSyncManager.execute(ASyncFactory.recordLoginInfo(loginUser.getUsername(), AuthCons.Login.LOGIN_OUT.getDes(), MessageTools.message("yaukie.user.logout.ok", new Object[0]), new Object[0]));
/* 109 */     return AjaxResult.success(AuthCons.Login.LOGIN_OUT.getDes());
/*     */   }
/*     */ }


/* Location:              /Users/rex/Desktop/x-common-auth-1.0.0-SNAPSHOT.jar!/org/yaukie/auth/service/impl/LoginHandlerServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */